package org.kisekiproject.evaluator.nodes {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	/**
	 * Handle if/else.
	 */
	public class IfNode extends EventDispatcher implements ISyntaxNode {

		private var _index:int;
		private var _ifNode:ISyntaxNode;
		private var _trueNode:ISyntaxNode;
		private var _elseNode:ISyntaxNode;

		/**
		 * Constructor.
		 */
		public function IfNode(ifNode:ISyntaxNode, trueNode:ISyntaxNode, elseNode:ISyntaxNode, index:int) {
			_ifNode=ifNode;
			_trueNode=trueNode;
			_elseNode=elseNode;
			_index=index;

			_ifNode.addEventListener(Event.CHANGE, onDependencyChange);
			_trueNode.addEventListener(Event.CHANGE, onDependencyChange);

			if (_elseNode)
				_elseNode.addEventListener(Event.CHANGE, onDependencyChange);
		}

		/**
		 * Dependency change.
		 */
		private function onDependencyChange(e:Event):void {
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		/**
		 * Get value.
		 */
		public function get value():Object {
			if (_ifNode.value)
				return _trueNode.value;

			if (_elseNode)
				return _elseNode.value;

			return null;
		}

		/**
		 * Get index.
		 */
		public function get index():int	{
			return _index;
		}
	}
}